#!/bin/bash
TARGET_DIR="/home/yun/Program/xv6Space/xv6-labs-2022"
MAKE_CMD="make qemu-gdb"
XTERM_FONT="-*-DejaVu Sans Mono-medium-r-normal--*-18-*-*-*-*-iso10646-1"

# 获取屏幕尺寸（假设1920x1080）
SCREEN_WIDTH=1920
SCREEN_HEIGHT=1080
WIN_WIDTH=1400
WIN_HEIGHT=800
POS_X=$(( (SCREEN_WIDTH - WIN_WIDTH) / 2 ))
POS_Y=$(( (SCREEN_HEIGHT - WIN_HEIGHT) / 2 ))

# 检查依赖项
if ! command -v xterm &> /dev/null; then
    echo -e "\033[31mError: Install xterm → sudo apt install xterm\033[0m" >&2
    exit 1
fi

# 启动终端（使用-fa参数更可靠）
xterm \
    -geometry 100x45+${POS_X}+${POS_Y} \
    -fa "DejaVu Sans Mono:size=20" \
    -fg "#333333" \
    -bg "#F6F6F6" \
    -title "XV6 Debug Console" \
    +bc \
    +sb \
    -ms "#007AFF" \
    -bw 0 \
    -e "cd \"$TARGET_DIR\" && echo '=== Starting QEMU Debug Environment ===' && $MAKE_CMD || { echo -e '\033[31mLaunch failed!\033[0m'; exec bash; }; exec bash" &

# 置顶窗口
if command -v wmctrl &> /dev/null; then
    sleep 0.5
    wmctrl -r "XV6 Debug Console" -b add,above
fi